#####################################################################
#####################################################################
##								   ##
##		+++++				+++++		   ##
## 		--------------<AntiPvP>--------------		   ##
##		+++++				+++++		   ##
##								   ##
##		<<<<<<<<<<<<<<--------->>>>>>>>>>>>>>		   ##
##	<<<<<<<<				     >>>>>>>>	   ##
##  <<<<<<       Project AntiPvP by SOADfan and Team       >>>>>>  ##
##	<<<<<<<<				     >>>>>>>>	   ##
##		<<<<<<<<<<<<<<--------->>>>>>>>>>>>>>		   ##
##								   ##
##								   ##
##	>v0.0.1 First idea only lvl punishment worked.		   ##
##								   ##
##	>v0.1.0	Added a so called "square check" which		   ##
##		punishes all players who PvP in it. 		   ##
##		NPC's don't get punished for killing 		   ##
##		players anymore.^^				   ##
##								   ##
##	>v0.2.5	Added 1 "Warning" for players...		   ##
##		If they still ignore the rules 30 		   ##
##		minutes of jail awaits them :P			   ##
##		Requires ngJail v1.1.9				   ##
##								   ##
##	>v0.3.5 Added a "ZoneCheck" so whole zones can		   ##
##		be checked for unallowed PvP.			   ##
##								   ##
##	>v0.3.6 Added command to turn AntiPvP on/off		   ##
##								   ##
##	>v0.4.0	Fixed a problem with jail. The time		   ##
##		to be in jail got replaced. Now it		   ##
##		gets stacked :P					   ##
##								   ##
##	>v0.5.5 Added more commands. GM's are now		   ##
##		allowed to set the jailtime and jail		   ##
##		can be disabled if necessary...			   ##
##		Allowed lvl difference can be set with		   ##
##		a command now. Added a command to list		   ##
##		all AntiPvP commands! (.antipvp)		   ##
##								   ##
##		There is a command for players too.		   ##
##		(.antipvp info)					   ##
##		Players can check the allowed lvl		   ##
##		difference, PvP forbidden places, the		   ##
##		jailtime they will receive and if		   ##
##		AntiPvP is active.				   ##
##								   ##
##	>v1.0.0 (Final version?) i have rewritten the		   ##
##		whole thing. Finally it's faster...		   ##
##		Added new commands for the admin, he can	   ##
##		create the checked areas now with ingame	   ##
##		commands :). So he doesnt have to change	   ##
##		the values in the arrays anymore. The		   ##
##		values are stored in text files in 		   ##
##		WoWEmu/AntiPvP/$filename.txt... There is	   ##
##		also a BackUp folder so the last area/zone	   ##
##		can be reset :). I wrote this tcl just for	   ##
##		those fucking noobs who dont stop to PvP	   ##
##		in forbidden zones and the livetest showed	   ##
##		that the number of rulebreaking PvP		   ##
##		reduced a lot. So Enjoy :)			   ##
##								   ##
##	>v2.0.0 Final version! (as long as i dont get other	   ##
##		ideas^^. There is finally a config to		   ##
##		change things :)! You can disable single	   ##
##		checks of AntiPvP for example			   ##
##		Level/Area/Zone checks. You can also decide	   ##
##		if all needed files for antipvp should be	   ##
##		autogenerated on startup(but u still have to 	   ##
##		make a folder called "AntiPvP" in your main	   ##
##		wowemu directory*). Config goes in		   ##
##		------>	    WoWEmu/scripts/conf/     <------	   ##
##		But you can create all missing files with	   ##
##		a command too. In this case disable the check	   ##
##		on startup of wowemu in the config.		   ##
##		Another thing added is the command		   ##
##		.antipvp event <minutes> <yards>		   ##
##		so you dont have to disable AntiPvP during 	   ##
##		events. People in this area dont get checked	   ##
##		for any requirements and dont get punished :)	   ##
##								   ##
##	>v2.1.0 The "Now i know how to create folders!"		   ##
##		version. All required folders/files are now	   ##
##		autogenerated by the tcl itself! :)		   ##
##								   ##
##								   ##
##					    (c) SOADfan and Team   ##
##								   ##
##								   ##
#####################################################################
#####################################################################


::Custom::HookProc "::WoWEmu::ModDR" {

   global PvPActive
   global LvLCheck

     if { $PvPActive == 1 && $LvLCheck == 1 } {

	if { [GetObjectType $mob] == 4 } {

	  global maxleveldiff

	   if { ([GetLevel $player] - [GetLevel $mob]) > $maxleveldiff || ([GetLevel $player] - [GetLevel $mob]) < -$maxleveldiff } {
	      return 1.
	   }

	}

     }

}


::Custom::HookProcAfter "::WoWEmu::OnPlayerDeath" { 

   global PvPJail
   global PvPActive
   global LoadCoords
   set Plevel [GetPlevel $killer]
   set KillerPlayer [GetObjectType $killer]

     if { $PvPActive == 1 } {
	
	if { $KillerPlayer == 4 && $Plevel < 2 } {

	   ::Custom::ReadOnline

	   global mapid
	   global zoneid
	   global atc
	   global ztc
	   global xtop
	   global yleft
	   global yright
	   global xbottom
	   global saytext
	   global sayzone
	   global EventOn
	   global EventPos
	   global AreaCheck
	   global ZoneCheck
	   global EventArea
	   global EventTime
	   global EventStart
	   global PvPJailTime

	   if { $LoadCoords == 1 } {

	   set LoadCoords 0

	   set rhandle [open "AntiPvP/namearea.txt" ]
	   gets $rhandle saytext
	   close $rhandle

	   set rhandle [open "AntiPvP/namezone.txt" ]
	   gets $rhandle sayzone
	   close $rhandle

	   set rhandle [open "AntiPvP/numberofzones.txt" ]
	   gets $rhandle ztc
	   close $rhandle

	   set rhandle [open "AntiPvP/zone.txt" ]
	   gets $rhandle zoneid
	   close $rhandle

	   set rhandle [open "AntiPvP/numberofareas.txt" ]
	   gets $rhandle atc
	   close $rhandle

	   set rhandle [open "AntiPvP/map.txt" ]
	   gets $rhandle mapid
	   close $rhandle

	   set rhandle [open "AntiPvP/xcoordsbig.txt" ]
	   gets $rhandle xbottom
	   close $rhandle

	   set rhandle [open "AntiPvP/xcoordssmall.txt" ]
	   gets $rhandle xtop
	   close $rhandle

	   set rhandle [open "AntiPvP/ycoordsbig.txt" ]
	   gets $rhandle yright
	   close $rhandle

	   set rhandle [open "AntiPvP/ycoordssmall.txt" ]
	   gets $rhandle yleft
	   close $rhandle

	   }

	   set CheckDone 0
	   set KillerPos [lindex [GetPos $player]]
	   set NameKiller [GetName $killer]
	   set NamePlayer [GetName $player]
	   set XPlayer [ lindex $KillerPos 1 ]
	   set YPlayer [ lindex $KillerPos 2 ]
	   set MapPlayer [ lindex $KillerPos 0 ]
	   set ZonePlayer [ lindex [::Custom::GetOnlineData $NamePlayer] 5 ]

	   if { $EventOn == 1 } {

	   if { [ clock seconds ] <= ($EventStart + $EventTime) } {

	   if { $MapPlayer == [ lindex $EventPos 0 ] && $XPlayer <= [expr ([ lindex $EventPos 1 ] + $EventArea)] && $XPlayer >= [expr ([ lindex $EventPos 1 ] - $EventArea)] && $YPlayer <= [expr ([ lindex $EventPos 2 ] + $EventArea)] && $YPlayer >= [expr ([ lindex $EventPos 2 ] - $EventArea)] } {
	   return
	   }

	   } else {
	   set EventOn 0
	   }

	   }

	   if { $ZoneCheck == 1 } {

	   for { set ZoneChecked 0 } { $ZoneChecked<$ztc } {incr ZoneChecked} {
	   set ZoneIDPlayer [ lindex $zoneid $ZoneChecked ]
	   set SayZonePlayer [ lindex $sayzone $ZoneChecked ]
		if { $ZoneIDPlayer == $ZonePlayer && $NamePlayer != $NameKiller } {
		set CheckDone 1
		set ZoneChecked $ztc
		Say $killer 0 "|cffff0000$NameKiller killed $NamePlayer in a non-pvp zone!"
		CastSpell $killer $killer 7
		Resurrect $player
		SetQFlag $player IsAlive
		ClearQFlag $player IsDead
		CastSpell $player $player 15007
		}
	   }

	   }

	    if { $CheckDone == 0 } {

	     for { set checked 0 } { $checked<$atc } {incr checked} {

		if { ( $XPlayer >= [ lindex $xtop $checked ] && $XPlayer <= [ lindex $xbottom $checked ] && $YPlayer >= [ lindex $yleft $checked ] && $YPlayer <= [ lindex $yright $checked ] && $MapPlayer == [ lindex $mapid $checked ] && $AreaCheck == 1 ) && $NamePlayer != $NameKiller && $CheckDone == 0 } {

		   set SayPlayer [ lindex $saytext $checked ]
		   Say $killer 0 "|cffff0000$NameKiller killed a player in $SayPlayer!"

			if { [::GetQFlag $killer IsWarned] == 1 && $PvPJail == 1 } {
			
			package require SQLdb

			if { ! [ info exists handle ] } {
			set ::SQLdb::handle [ ::SQLdb::openSQLdb ]
			}

			set handle $::SQLdb::handle
			set pos [ ::GetPos $killer ]
			set jaildate [clock seconds]

			if { ! [ ::SQLdb::booleanSQLdb $::ngjail::handle "SELECT `jailtime` FROM `ngjail` WHERE (`name` = '$NameKiller')" ] } {
			::SQLdb::querySQLdb $::ngjail::handle "INSERT INTO `ngjail` (`name`,`PlayerUID`, `jaildate`, `jailtime`, `pos`, `reason`, `jail`, `by` ) VALUES('$NameKiller','$killer', '$jaildate', '$PvPJailTime', '[ ::SQLdb::canonizeSQLdb $pos ]', 'disrespecting the rules', 1 ,'anti-pvp')"
			set totaltimeadd [ expr ($PvPJailTime / 1)]
			} else {
			set exijailtime [::SQLdb::firstcellSQLdb $handle "SELECT jailtime FROM `ngjail` WHERE (`name` = '$NameKiller')"]
			set totaltimeadd [ expr ($exijailtime + $PvPJailTime)]
			::SQLdb::querySQLdb $::ngjail::handle "UPDATE `ngjail` SET `PlayerUID`= '$killer', `jaildate` = '$jaildate', `jailtime` = '$totaltimeadd', `pos` = '[ ::SQLdb::canonizeSQLdb $pos ]', `reason` = 'disrespecting the rules', `jail` = 1, `by` = 'anti-pvp' WHERE (`name` = '$NameKiller')"
			}

			set jailmins [ expr ($totaltimeadd / 60)]
			Say $killer 0 "I am jailed for $jailmins minutes because i discrespected the rules! (PvP)"
			Teleport $killer 13 248.952652 228.112503 -1131.864624
			}

		set checked $atc
		CastSpell $killer $killer 7
		Resurrect $player
		SetQFlag $player IsAlive
		ClearQFlag $player IsDead
		CastSpell $player $player 15007

			if { [::GetQFlag $killer IsWarned] == 0 } {
			Say $killer 0 "|cffff0000$NameKiller you are warned now! Next time you'll be jailed and you won't get warned again!"
			SetQFlag $killer IsWarned
			}

		}
	     }
	    }
	}
     }

}